<?php


namespace app\api\controller;

use app\BaseController;
use app\common\facade\auth\Auth;
use app\middleware\ymsh\SighCheck;
use app\model\Agent;
use think\facade\Cache;
use think\Request;
use app\model\Config;

class Rebate extends BaseController
{
    protected $middleware = [
        \app\middleware\ApiChannel::class,
        \app\middleware\ApiUserAuthV2::class => ['except' => ['phoneInfo']],
    ];

    //恋上梧桐
    public $lswtUrl;

    //呆萌价
    public $dmjUrl;

    protected function initialize()
    {
        parent::initialize();
        $this->lswtUrl = env('app.lswt_url');
        $this->dmjUrl = Config::query()->where('type','profit_share')->where('field', 'profit_share_request_url')->value('value');
    }

    /**
     * 恋上梧桐
     * 恋上梧桐查询代理返佣比例
     * @return \think\response\Json
     */
    public function lswtRebate(Request $request)
    {
        $type = $request->param('type');
        $agent_id = Auth::agentId();
        //$merchant_id = Auth::merchantId();
        $agent = Agent::where('id', $agent_id)->field('id, status, merchant_id')->find();
        if (!$agent) {
            return fail('查询失败，代理不存在');
        }
        if ($agent->status == Agent::STATUS_CLOSE) {
            return fail('查询失败，代理已被禁用');
        }
        if (empty($type)) {
            return fail('查询类型不能为空');
        }
        //固定写死
        $merchant_id = 2;
        $cache_key = $merchant_id.$request->action().$type;
        $result =  Cache::get($cache_key);
        if ($result) {
            return success('查询成功', $result);
        }
        $param = [
            'merchantId'  => $merchant_id,
            'type'        => $type,
        ];
        //请求java查询返佣比例
        $sign = (new SighCheck())->sign($param);
        $url = $this->lswtUrl.'/web-dmhw/micro/commission/provide/getCommissionRatio?sign='.$sign.'&merchantId='.$merchant_id.'&type='.$type;
        $res = curlGet($url);
        $result = json_decode($res, true);
        if (!isset($result['code']) || $result['code'] != 200) {
            return success('查询成功', ['selfPurchaseRatio' => null]);
        }
        $row = [
            'selfPurchaseRatio' => $result['data']['selfPurchaseRatio']??null,
        ];
        Cache::set($cache_key, $row, 5*60);
        return success('查询成功', $row);
    }

    /**
     * 呆萌价
     * 呆萌价查询用户返佣比例
     * @return \think\response\Json
     */
    public function dmjRebate(Request $request)
    {
        $userId = $request->param('user_id');
        $type = $request->param('type');
        if (empty($userId) || empty($type)) {
            return fail('用户或者类型不能为空');
        }
//        $cache_key = $userId.$request->action().$type;
//        $result =  Cache::get($cache_key);
//        if ($result) {
//            return success('查询成功', $result);
//        }
        $param = [
            'userId'  => $userId,
            'type'    => $type,
        ];
        //请求java查询返佣比例
        $sign = (new SighCheck())->sign($param);
        $url = $this->dmjUrl.'/web-dmhw/micro/commission/provide/getCommissionRatio?sign='.$sign.'&userId='.$userId.'&type='.$type;
        $result = curlGet($url);
        $res = json_decode($result, true);
        if (!isset($res['code']) || $res['code'] != 200) {
            return success('查询成功', ['selfPurchaseRatio' => null]);
        }
        $row = [
            'selfPurchaseRatio' => $res['data']??null,
        ];
        //Cache::set($cache_key, $row, 5*60);
        return success('查询成功', $row);
    }
}